package com.qingfeng.activiti.controller;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.qingfeng.activiti.service.ITobereadService;
import com.qingfeng.base.controller.BaseController;
//import com.qingfeng.common.service.SkdmClient;
import com.qingfeng.entity.MyResponse;
import com.qingfeng.entity.QueryRequest;
import com.qingfeng.entity.activiti.Toberead;
import com.qingfeng.exception.MyException;
import com.qingfeng.utils.*;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName TobereadController
 * @author Administrator
 * @version 1.0.0
 * @Description 流程待阅
 * @createTime 2022/12/24 0024 12:00
 */
@Slf4j
@Validated
@RestController
@RequestMapping(value = "/activiti/toberead")
public class TobereadController extends BaseController {

	@Autowired
	private ITobereadService tobereadService;

//	@Autowired
//	private SkdmClient skdmClient;


	/**
	 * @title findListPage
	 * @description 查询数据分页列表
	 * @author Administrator
	 * @updateTime 2022/12/24 0024 12:13
	 */
	@GetMapping("/findListPage")
	public MyResponse findListPage(QueryRequest queryRequest, Toberead toberead) {
		String userParams = SecurityContextHolder.getContext().getAuthentication().getName();
		//处理数据权限
		//String user_id = userParams.split(":")[1];
		String user_id = userParams.split(":")[0];//okms用户
		toberead.setUser_id(user_id);
		Map<String, Object> dataTable = MyUtil.getDataTable(tobereadService.findListPage(toberead, queryRequest));
		return new MyResponse().data(dataTable);
	}


	@GetMapping("/findMyListPage")
	public MyResponse findMyListPage(QueryRequest queryRequest, Toberead toberead) {
		String userParams = SecurityContextHolder.getContext().getAuthentication().getName();
		//处理数据权限
		//String user_id = userParams.split(":")[1];
		String user_id = userParams.split(":")[0];//okms用户
		toberead.setCreate_user(user_id);
		Map<String, Object> dataTable = MyUtil.getDataTable(tobereadService.findListPage(toberead, queryRequest));
		return new MyResponse().data(dataTable);
	}





	/**
	 * @title findList
	 * @description 查询数据列表
	 * @author Administrator
	 * @updateTime 2022/12/24 0024 12:13
	 */
	@GetMapping("/findList")
	public MyResponse findList(QueryRequest queryRequest, Toberead toberead) {
		String userParams = SecurityContextHolder.getContext().getAuthentication().getName();
		//处理数据权限
		String user_id = userParams.split(":")[1];
		toberead.setUser_id(user_id);
		List<Toberead> list = tobereadService.findList(toberead);
		return new MyResponse().data(list);
	}


	/**
	 * @title save
	 * @description 保存数据
	 * @author Administrator
	 * @updateTime 2022/12/24 0024 12:13
	 */
	@PostMapping("/saveToberead")
	public void save(@Valid @RequestBody Toberead toberead, HttpServletResponse response) throws Exception {
		Json json = new Json();
		try {
			String userIds[] = toberead.getUserIds().split(",");
			toberead.setIs_read("0");
			toberead.setType("0");
			for (String user_id:userIds) {
				toberead.setUser_id(user_id);
				toberead.setId(GuidUtil.getUuid());
				toberead.setCreate_time(DateTimeUtil.getDateTimeStr());
				String authParams = SecurityContextHolder.getContext().getAuthentication().getName();
				toberead.setCreate_user(authParams.split(":")[1]);
				toberead.setCreate_organize(authParams.split(":")[2]);
				this.tobereadService.save(toberead);
			}
			json.setSuccess(true);
			json.setMsg("新增信息成功");
		} catch (Exception e) {
			String message = "新增信息失败";
			json.setSuccess(false);
			json.setMsg(message);
			e.printStackTrace();
			throw new MyException(message);
		}
		this.writeJson(response,json);
	}

	/**
	 * @title update
	 * @description 更新数据
	 * @author Administrator
	 * @updateTime 2022/12/24 0024 12:13
	 */
	@PutMapping
	public void update(@Valid @RequestBody Toberead toberead,HttpServletResponse response) throws Exception {
		Json json = new Json();
		try {
			this.tobereadService.updateById(toberead);
			json.setSuccess(true);
			json.setMsg("修改信息成功");
		} catch (Exception e) {
			String message = "修改信息失败";
			json.setSuccess(false);
			json.setMsg(message);
			throw new MyException(message);
		}
		this.writeJson(response,json);
	}

	/**
	 * @title delete
	 * @description 删除数据
	 * @author Administrator
	 * @updateTime 2022/12/24 0024 12:14
	 */
	@DeleteMapping("/{ids}")
	public void delete(@NotBlank(message = "{required}") @PathVariable String ids, HttpServletResponse response) throws Exception {
		Json json = new Json();
		try {
			String[] del_ids = ids.split(StringPool.COMMA);
			this.tobereadService.removeByIds(Arrays.asList(del_ids));
			json.setSuccess(true);
			json.setMsg("删除信息成功");
		} catch (Exception e) {
			String message = "删除信息失败";
			json.setSuccess(false);
			json.setMsg(message);
			throw new MyException(message);
		}
		this.writeJson(response,json);
	}


	/**
	 * 更新为已读
	 * @param toberead
	 * @param response
	 * @throws Exception
	 */
	@PostMapping("/upRead")
	public void upRead(@Valid @RequestBody Toberead toberead, HttpServletResponse response) throws Exception {
		Json json = new Json();
		try {
			toberead.setIs_read("1");
			toberead.setRead_time(DateTimeUtil.getDateTimeStr());
			this.tobereadService.updateById(toberead);
			json.setSuccess(true);
			json.setMsg("新增信息成功");
		} catch (Exception e) {
			String message = "新增信息失败";
			json.setSuccess(false);
			json.setMsg(message);
			e.printStackTrace();
			throw new MyException(message);
		}
		this.writeJson(response,json);
	}


//	@GetMapping("/releaseDB")
//	@ApiOperation(value = "发布库",notes = "发布库")
//	public void releaseDeptDB()
//	{
//		ResponseResult responseResult = skdmClient.releaseDataBase("KDM_1686022607856_127");
//		System.out.println("responseResult = " + responseResult);
//
//	}


}
